package com.easy.util;

import com.easy.util.ExpiryMap;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class VerificationCodeService {

    private final ExpiryMap codeStorage = new ExpiryMap(5 * 60 * 1000); // 5分钟有效期

    // 生成6位随机验证码
    public String generateCode() {
        Random random = new Random();
        return String.format("%06d", random.nextInt(1000000));
    }

    // 存储验证码
    public void storeCode(String key, String code) {
        codeStorage.put(key, code);
    }

    // 验证验证码
    public boolean verifyCode(String key, String inputCode) {
        Object storedCode = codeStorage.get(key);
        return storedCode != null && storedCode.equals(inputCode);
    }

    // 移除验证码
    public void removeCode(String key) {
        codeStorage.remove(key);
    }
}
